package com.ctcemti.mdm.common.util;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import eu.bitwalker.useragentutils.UserAgent;
import javax.servlet.http.HttpServletRequest;

import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.Version;

/**
 * Copyright © 2018 YouoTech Of China,All Right Reserved.
 *
 * @PackageName: com.youotech.olm.util
 * @ProjectName: olm
 * @Description:
 * @version: v1.0.0
 * @author: LiNan Email:linan@youotech.com
 * @date: 2018/11/12 14:44
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ---------------------------------------------------------*
 * 2018/11/12     linan           v1.0.0               修改原因
 */
@Slf4j
public class MacUtils {

    public static String getLocalMac(InetAddress ia) {
        // TODO Auto-generated method stub
        //获取网卡，获取地址
        byte[] mac;
        try {
            mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
        } catch (SocketException e) {
            log.error("获取mac地址失败！");
            throw new Error();
        }
        System.out.println("mac数组长度：" + mac.length);
        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i < mac.length; i++) {
            if (i != 0) {
                sb.append("-");
            }
            //字节转换为整数
            int temp = mac[i] & 0xff;
            String str = Integer.toHexString(temp);
            System.out.println("每8位:" + str);
            if (str.length() == 1) {
                sb.append("0").append(str);
            } else {
                sb.append(str);
            }
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 获取客户端ip
     * <p>Title: getIpAddress</p>  
     * <p>Description: </p>  
     * @author bxf  
     * @date 2019年3月8日 下午2:33:03    
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request) {
    	String ip = request.getHeader("x-forwarded-for"); 
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {  
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if( ip.indexOf(",")!=-1 ){
                ip = ip.split(",")[0];
            }
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
    /**
     * 获取浏览器信息
     * <p>Title: getClientOS</p>  
     * <p>Description: </p>  
     * @author bxf  
     * @date 2019年3月8日 下午4:02:27    
     * @param userAgent
     * @return
     */
    public static String getBrowser(HttpServletRequest request){
    	//获取浏览器信息
    	Browser browser = UserAgent.parseUserAgentString(request.getHeader("User-Agent")).getBrowser();
    	if(Browser.UNKNOWN.equals(browser)){
    		return "未知浏览器登录";
    	}else{
    		//获取浏览器版本号
        	Version version = browser.getVersion(request.getHeader("User-Agent"));
        	String info = browser.getName() + "/" + version.getVersion();
        	return info;
    	}
    	
    	
    }
}
